﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Simple.Common.Core.Extensions
{
    public static class DateTimeExtensions
    {
        /// <summary>
        /// 用于计算时间戳的时间值
        /// </summary>
        private static DateTime UnixTimestamp = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        /// <summary>
        /// 将当前时间的值转换为时间戳
        /// </summary>
        /// <param name="time">需要转换的时间</param>
        /// <returns>从1970-01-01 0:0:0开始计算的总毫秒数</returns>
        public static long ToTimestamp(this DateTime time)
        {
            return (long)time.ToUniversalTime().Subtract(UnixTimestamp).TotalMilliseconds;
        }

        /// <summary>
        /// 转换为基础时间格式
        /// </summary>
        /// <param name="time"></param>
        /// <returns></returns>
        public static string ToCommon(this DateTime time)
        {
            return time.ToString("yyyy-MM-dd HH:mm:ss");
        }


    }
}
